package leave.model.impl.mysql;

import java.io.Serializable;

import java.sql.Time;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import leave.dataaccess.DAOFactory;
import leave.exception.DataAccessException;
import leave.model.CompensationLeaveLedger;
import leave.model.Employee;
import leave.model.Leave;

import leave.status.*;

public class EmployeeImpl implements Serializable, Employee {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1344011256199686474L;

	private int employeeID;
	private String name;
	private String password;
	private String role;
	private String email;
	private Employee reportsTo;
	private String rank;
	private ArrayList<Integer> leaveTakenID;
	private ArrayList<Integer> compensationsClaimed;
	protected DAOFactory DAOF;
	private HashMap<Integer, Float> compensatableDays;

	public HashMap<Integer, Float> getCompensatableDays() {
		return compensatableDays;
	}

	public void setCompensatableDays(HashMap<Integer, Float> compensatableDays) {
		this.compensatableDays = compensatableDays;
	}

	public ArrayList<Integer> getCompensationsClaimed() {
		return compensationsClaimed;
	}

	public void setCompensationsClaimed(ArrayList<Integer> compensationsClaimed) {
		this.compensationsClaimed = compensationsClaimed;
	}

	public ArrayList<Integer> getLeaveTakenID() {
		return leaveTakenID;
	}

	public void setLeaveTakenID(ArrayList<Integer> leaveTakenID) {
		this.leaveTakenID = leaveTakenID;
	}

	public EmployeeImpl() throws DataAccessException {
		super();
		leaveTakenID = new ArrayList<Integer>();
		// compensationsClaimed = new ArrayList<CompensationLeaveLedger>();
		// compensatableDays = new ArrayList<CompensatableDays>();
		DAOF = DAOFactory.getInstance();
	}

	@Override
	public int getEmployeeID() {
		return employeeID;
	}

	@Override
	public void setEmployeeID(int employeeID) {
		this.employeeID = employeeID;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String getRole() {
		return role;
	}

	@Override
	public void setRole(String role) {
		this.role = role;
	}

	@Override
	public String getEmail() {
		return email;
	}

	@Override
	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public Employee getReportsTo() {
		return reportsTo;
	}

	@Override
	public void setReportsTo(Employee reportsTo) {
		this.reportsTo = reportsTo;
	}

	@Override
	public String getRank() {
		return rank;
	}

	@Override
	public void setRank(String value) {
		this.rank = value;
	}

	@Override
	public void applyLeave(String leaveType, Date startDate, Date endDate,
			String employeeComment, String workDissemination,
			String contactDetails) {
		// TODO Auto-generated method stub
		Leave leaveApplication = new LeaveImpl();
		leaveApplication.setLeaveType(leaveType);
		leaveApplication.setStartDate(startDate);
		leaveApplication.setEndDate(endDate);
		leaveApplication.setEmployeeComment(employeeComment);
		leaveApplication.setWorkDissemination(workDissemination);
		leaveApplication.setContactDetails(contactDetails);
		leaveApplication.setApprovedBy(this.reportsTo);

		LeaveStatus appStatus;
		if (this.reportsTo == null)
			appStatus = LeaveStatus.APPROVED;
		else
			appStatus = LeaveStatus.APPLIED;

		leaveApplication.setStatus(appStatus);

		leaveTakenID.add(leaveApplication.getLeaveID());

	}

	@Override
	public void claimCompensationLeave(Date dateOfOvertime, Time startTime,
			Time endTime) {
		// TODO Auto-generated method stub
		CompensationLeaveLedger ledger = new CompensationLeaveLedgerImpl();
		ledger.setDateOfOvertime(dateOfOvertime);
		ledger.setStartTime(startTime);
		ledger.setEndTime(endTime);
		ledger.setApprovedBy(reportsTo);

		CompensationClaimStatus ccs = null;
		if (this.reportsTo == null) {
			ccs = CompensationClaimStatus.APPROVED;
		} else {
			if ((int) (endTime.getTime() - endTime.getTime()) >= 4 * 3600)
				ccs = CompensationClaimStatus.QUALIFIED;
			else
				ccs = CompensationClaimStatus.NOT_QUALIFIED;
		}
		ledger.setStatus(ccs);

		compensationsClaimed.add(ledger.getTransactionID());
	}
}
